//
//  WishWebService.m
//  eGiftr
//
//  Created by Phien Tram on 9/5/13.
//  Copyright (c) 2013 eGiftr. All rights reserved.
//

#import "WishWebService.h"
#import "ServiceNameMapper.h"
#import "SBJson.h"
#import "NSDictionary+Utility.h"
#import "WebServiceDataConverter.h"
#import "FindKeyword.h"

@implementation WishWebService

- (id)initFindKeywords:(id)delegate query:(NSString *)query cate:(NSString *)cate appToken:(NSString *)appToken
{
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                            query, @"query",
                            cate, @"cate",
                            appToken, @"appToken",
                            nil];
    self = [super initWithData:params serviceName:[ServiceNameMapper findKeywords] method:@"GET"];
    if (self) {
        self.delegate = delegate;
        
        [self request];
    }
    return self;
}

+ (id)findKeywords:(id)delegate query:(NSString *)query cate:(NSString *)cate appToken:(NSString *)appToken
{
    return [[self alloc] initFindKeywords:delegate query:query cate:cate appToken:appToken];
}

- (id)initGetRecommendedKeywords:(id)delegate appToken:(NSString *)appToken
{
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                            appToken, @"appToken",
                            nil];
    self = [super initWithData:params serviceName:[ServiceNameMapper getRecommendedKeywords] method:@"GET"];
    if (self) {
        self.delegate = delegate;
        
        [self request];
    }
    return self;
}

+ (id)getRecommendedKeywords:(id)delegate appToken:(NSString *)appToken
{
    return [[self alloc] initGetRecommendedKeywords:delegate appToken:appToken];
}

- (id)initGetWish:(id)delegate wishId:(NSString *)wishId appToken:(NSString *)appToken
{
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                            wishId, @"id",
                            appToken, @"appToken",
                            nil];
    self = [super initWithData:params serviceName:[ServiceNameMapper getWishById] method:@"GET"];
    if (self) {
        self.delegate = delegate;
        
        [self request];
    }
    return self;
}

+ (id)getWish:(id)delegate wishId:(NSString *)wishId appToken:(NSString *)appToken
{
    return [[self alloc] initGetWish:delegate wishId:wishId appToken:appToken];
}

- (id)initGetWishListByUserId:(id)delegate userId:(NSString *)userId appToken:(NSString *)appToken page:(NSNumber *)page
{
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                            userId, @"userId",
                            appToken, @"appToken",
                            page, @"page",
                            nil];
    self = [super initWithData:params serviceName:[ServiceNameMapper getWishListByUserId] method:@"GET"];
    if (self) {
        self.delegate = delegate;
        
        [self request];
    }
    return self;
}

+ (id)getWishListByUserId:(id)delegate userId:(NSString *)userId appToken:(NSString *)appToken page:(NSNumber *)page
{
    return [[self alloc] initGetWishListByUserId:delegate userId:userId appToken:appToken page:page];
}

- (id)initSaveWishesByUserId:(id)delegate userId:(NSString *)userId keywords:(NSArray *)keywords appToken:(NSString *)appToken
{
    NSMutableArray *mutableArray = [NSMutableArray array];
    for (FindKeyword *keyword in keywords) {
        NSDictionary *wish = [NSDictionary dictionaryWithObjectsAndKeys:
                              userId, @"userId",
                              keyword.keyword, @"keyword",
                              keyword.cateName, @"cate",
                              keyword.subCateName, @"subCate",
                              nil];
        [mutableArray addObject:wish];
    }
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                            [NSArray arrayWithArray:mutableArray], @"data",
                            appToken, @"appToken",
                            nil];
    NSLog(@"saveWishesByUserId: %@", params);
    self = [super initWithData:params serviceName:[ServiceNameMapper saveWishesByUserId] method:@"POST"];
    if (self) {
        self.delegate = delegate;
        
        [self request];
    }
    return self;
}

+ (id)saveWishesByUserId:(id)delegate userId:(NSString *)userId keywords:(NSArray *)keywords appToken:(NSString *)appToken
{
    return [[self alloc] initSaveWishesByUserId:delegate userId:userId keywords:keywords appToken:appToken];
}

- (BOOL)parse:(NSString **)error code:(NSInteger *)code
{
	id jResult = [self.receivedData JSONValue];
    NSLog(@"jResult: %@", [jResult description]);
    BOOL success = [[jResult objectForKey:@"success"] boolValue];
    if (success)
    {
        error = nil;
        if ([self.serviceName isEqualToString:[ServiceNameMapper getWishListByUserId]]) {
            self.result = [WebServiceDataConverter convertJsonDataToMyWishList:jResult];
        } else if ([self.serviceName isEqualToString:[ServiceNameMapper findKeywords]]
                   || [self.serviceName isEqualToString:[ServiceNameMapper getRecommendedKeywords]]) {
            self.result = [WebServiceDataConverter convertJsonDataToFindKeywords:jResult];
        } else {
            self.result = jResult;
        }
    }
    else
    {
        NSDictionary *errorDictionary = [jResult objectOrNilForKey:@"error"];
        NSString *message = [errorDictionary objectOrNilForKey:@"errorMessage"];
        NSString *errorDetails = [errorDictionary objectOrNilForKey:@"details"];
        if (message) {
            *error = message;
        } else if (errorDetails) {
            *error = errorDetails;
        }
        NSNumber *errorCode = [errorDictionary objectOrNilForKey:@"errorCode"];
        *code = [errorCode integerValue];
        return NO;
    }
    
    
	return YES;
}


@end
